package com.easy.base.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import org.apache.commons.lang.StringUtils;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.easy.core.dbHandle.entity.BaseEntity;

@Entity
@Table(name = "BASE_SYS_CONFIG")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class SysConfig extends BaseEntity implements Comparable<SysConfig> {

	private static final long serialVersionUID = 1L;
	
	@Id
	private @Column(name="ID")  			String		id;
	private @Column(name="CON_NAME")		String		name;			//配置名称
	private @Column(name="CON_DESC")		String 		desc;			//配置描述
	private @Column(name="CON_VALUE")		String 		value;			//数据值
	private @Column(name="CON_DATA_TYPE")	Short 		dataType;		//数据类型,1=string,2=boolean,3=int,4=float,5=datetime,6=date,7=time

	public SysConfig(){}
	
	public SysConfig(String id, String value){
		this.id = id;
		this.value = value;
		this.generateDataType();
	}
	
	public SysConfig(String id, String value, String desc){
		this.id = id;
		this.value = value;
		this.desc = desc;
		this.generateDataType();
	}
	
	public void generateDataType(){
		this.dataType = 1;
		if(StringUtils.isBlank(value)) this.dataType = 1;
		else if("true".equalsIgnoreCase(value)||"false".equalsIgnoreCase(value)) this.dataType = 2;
		else if(value.matches("^-?\\d+$")) this.dataType = 3;
		else if(value.matches("^-?(\\d*\\.\\d+|\\d+\\.\\d*)$")) this.dataType = 4;
		else if(value.matches("^\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}$")) this.dataType = 5;
		else if(value.matches("^\\d{4}-\\d{2}-\\d{2}$")) this.dataType = 6;
		else if(value.matches("^\\d{2}:\\d{2}:\\d{2}$")) this.dataType = 7;
	}
	
	public int compareTo(SysConfig o) {
		return getId().compareTo(o.getId());
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public Short getDataType() {
		return dataType;
	}

	public void setDataType(Short dataType) {
		this.dataType = dataType;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
}
